package com.usian.admin.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 自动封装token 到feign请求头中。
 */
@Configuration
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 通过请求上下文对象  获取token
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token =  attributes.getRequest().getHeader("token");
//        token = "eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAADWLQQrDMAzA_uJzA6vtOml_4yU9ZLAScAIbpX-fe9hNQuiEV6-wATITq0hQzhyYlYIi5xDLIyciXAolmKBqh20WkZiWecUJbDz9tq_1_X13M9dhVQ83HcVNW3PeP-1_pnif1RteP8m-bLKAAAAA.8KO_jBeWAlKyYUP3sK7q8CJTYl42L7QL7pfBqMjLoaMO046r8EVGu-aQlIfi5hZ26Zua0_gBjFM-7yguWUylcA";
        // 获取token 然后放到请求头中
        requestTemplate.header("token",token);
    }

}
